package com.github.icloudpay.crm.core.biz;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.github.icloudpay.crm.core.client.FlyrayBizService;
import com.github.icloudpay.crm.core.entity.MerchantAccount;
import com.github.icloudpay.crm.core.entity.MerchantBase;
import com.github.icloudpay.crm.core.entity.MerchantBaseExt;
import com.github.icloudpay.crm.core.mapper.MerchantAccountMapper;
import com.github.icloudpay.crm.core.mapper.MerchantBaseExtMapper;
import com.github.icloudpay.crm.core.mapper.MerchantBaseMapper;
import com.github.wxiaoqi.security.common.biz.BaseBiz;
import com.github.wxiaoqi.security.common.msg.AccountType;
import com.github.wxiaoqi.security.common.msg.ResponseCode;

/**
 * 商户基础信息
 *
 * @author centerroot
 * @email lfw6699@163.com
 * @date 2018-05-25 15:20:48
 */
@Service
public class MerchantBaseBiz extends BaseBiz<MerchantBaseMapper,MerchantBase> {
	private static final Logger logger = LoggerFactory.getLogger(MerchantBaseBiz.class);

	@Autowired
	private MerchantBaseMapper merchantBaseMapper;
	@Autowired
	private MerchantBaseExtMapper merchantBaseExtMapper;
	@Autowired
	private MerchantAccountMapper merchantAccountMapper;
	@Autowired
	private FlyrayBizService flyrayBizService;
	@Value("${bountyHunter.acceptBizNo}")
	private String acceptBizNo;

	
	/**
	 * 获取悬赏中心统计数据
	 */
	public Map<String, Object> getMerStatistics(Map<String, Object> param){
		logger.info("获取猎手中心统计数据 请求参数：{}",param);
		Map<String, Object> respMap = new HashMap<String, Object>();
		String merId = (String) param.get("merId");
		MerchantBase merchantBase = merchantBaseMapper.selectByPrimaryKey(merId);
		if (null != merchantBase) {
			// 查询用户可用悬赏币
			MerchantAccount reqMerAccBalance = new MerchantAccount();
			reqMerAccBalance.setMerId(merId);
			reqMerAccBalance.setAccType(AccountType.ACC_BALANCE.getCode());// 余额账户
			MerchantAccount respMerAccBalance = merchantAccountMapper.selectOne(reqMerAccBalance);
			if (null != respMerAccBalance && null != respMerAccBalance.getAccBalance()) {
				respMap.put("accBalance", respMerAccBalance.getAccBalance());
			}else{
				respMap.put("accBalance", "0.00");
			}
			// 查询用户冻结悬赏币
			MerchantAccount reqMerAccFreeze = new MerchantAccount();
			reqMerAccFreeze.setMerId(merId);
			reqMerAccFreeze.setAccType(AccountType.ACC_FREEZE.getCode());// 冻结账户
			MerchantAccount respMerAccFreeze = merchantAccountMapper.selectOne(reqMerAccFreeze);
			
			if (null != respMerAccFreeze && null != respMerAccFreeze.getAccBalance()) {
				respMap.put("freezeBalance", respMerAccFreeze.getAccBalance());
			}else{
				respMap.put("freezeBalance", "0.00");
			}
			
			MerchantBaseExt merchantBaseExt = merchantBaseExtMapper.selectByPrimaryKey(merId);
			
			if (null != merchantBaseExt && null != merchantBaseExt.getInterview()) {
				respMap.put("interview", merchantBaseExt.getInterview());
			}else{
				respMap.put("interview", "0");
			}
			// 收到待查看简历、进行中的任务
			Map<String, Object> reqRewardMap = new HashMap<String, Object>();
			reqRewardMap.put("merNo", merId);
			Map<String, Object> rewardMap = flyrayBizService.getRewardCenterInfo(reqRewardMap);
			if (null != rewardMap && ResponseCode.OK.getCode().equals(rewardMap.get("code"))) {
				respMap.put("noViewedCount", rewardMap.get("noViewedCount"));
				respMap.put("runningCount", rewardMap.get("runningCount"));
			}else{
				respMap.put("noViewedCount", "0");
				respMap.put("runningCount", "0");
			}
			
			respMap.put("code", ResponseCode.OK.getCode());
			respMap.put("msg", ResponseCode.OK.getMessage());
		}else{
			respMap.put("code", ResponseCode.MER_NOTEXIST.getCode());
			respMap.put("msg", ResponseCode.MER_NOTEXIST.getMessage());
		}
		
		return respMap;
	}
	
}